package com.guan.study.poi.write;


import com.guan.study.poi.UseCaseExportVo;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.beans.PropertyDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2023/7/1 1:27
 * @Author: yaobai
 * @Description:
 */
public class Write02 {


    public static void main(String[] args) throws Exception {

        exportExcel02(data());

    }


    private static void exportExcel02(List<UseCaseExportVo> vos) throws Exception {
        //表头数据
        String[] head = new String[]{
                "用例编号","功能模块","验证点","前置条件","操作步骤","预期结果","编写人","备注",
                "开发用例执行结果","开发更新时间","开发完成人姓名","开发备注",
                "测试用例执行结果","测试更新时间","测试完成人姓名","测试备注",
                "灰度用例执行结果","灰度更新时间","灰度完成人姓名","灰度备注",
                "生产用例执行结果","生产更新时间","生产完成人姓名","生产备注"
        };
        //获取对象所有属性
        Field[] fields = UseCaseExportVo.class.getDeclaredFields();
        for (Field field : fields) {
            //设置可见性
            field.setAccessible(true);
        }
        List<String> headers = Arrays.asList(head);

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 创建工作簿对象
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("Sheet1");
        // 设置内容和样式
        for (int rowIdx = 0; rowIdx < vos.size(); rowIdx++) {
            //创建行
            Row row = sheet.createRow(rowIdx);
            //获取导出数据对象
            UseCaseExportVo vo = vos.get(rowIdx);
            for (int colIdx = 0; colIdx < 24; colIdx++) {
                //创建每行的单元格
                Cell cell = row.createCell(colIdx);
                if (rowIdx == 0){
                    //设置表头单元格内容
                    cell.setCellValue(headers.get(colIdx));
                }else if (rowIdx == 1){
                    //设置表头单元格内容
                    if (colIdx >= 8){
                        cell.setCellValue(headers.get(colIdx));
                    }
                }else {
                    //设置表体单元格内容
                    PropertyDescriptor pd = new PropertyDescriptor(fields[colIdx].getName(), UseCaseExportVo.class);
                    Method getMethod = pd.getReadMethod();
                    Object invoke = getMethod.invoke(vo);
                    if (invoke instanceof Date){
                        cell.setCellValue(df.format((Date) invoke));
                    }else {
                        cell.setCellValue(String.valueOf(invoke == null ? "" : invoke));
                    }

                }


                // 设置单元格样式
                CellStyle style = workbook.createCellStyle();
                style.setAlignment(HorizontalAlignment.CENTER);
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                if (colIdx < 8){
                    sheet.setColumnWidth(colIdx,15 * 256);
                }else {
                    sheet.setColumnWidth(colIdx,20 * 256);
                }
                cell.setCellStyle(style);
            }
        }

        // 合并单元格 第1-7列与下一行合并
        for (int i = 0; i < 8; i++) {
            sheet.addMergedRegion(new CellRangeAddress(0, 1, i, i));
        }

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 8, 11));
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 12, 15));
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 16, 19));
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 20, 23));


        // 输出到文件
        try (FileOutputStream outputStream = new FileOutputStream("E:\\WorkDirTest2\\用例导出.xlsx")) {
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Excel 文件已生成。");

    }

    private static List<UseCaseExportVo> data(){

        List<UseCaseExportVo> list = new ArrayList<>();

        for (int i = 0; i < 20; i++) {

            UseCaseExportVo vo =
                UseCaseExportVo.builder()
                        .hdRemark("灰度备注")
                        .caseNo("100"+i)
                        .editor("李四"+i)
                        .devFinishUserName("王五"+i)
                        .devRemark("开发备注")
                        .devResult("完成")
                        .devUpdateTime(new Date())
                        .huUser("篮下只因")
                        .module("项目管理")
                        .preResult("全部完成")
                        .testUpdateTime(new Date())
                        .build();

            list.add(vo);
        }

        return list;
    }
}
